/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('MainCtrl', ['$scope','$rootScope', '$timeout',  'MainService', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, MainService, ionicMaterialMotion, ionicMaterialInk) {

    var init = function(){
        //判断用户是否登陆
        if(!$rootScope.localStorageObj){
            $rootScope.openLoginModal();
        }else{
            loadData();
        }
    },
    loadData = function(){
         MainService.getHotelCount()
        .success(function(res){
            if(res.success && res.attribute){
                $scope.hotel = res.attribute;
            }else{
                console.log();
            }
        }).error(function(error){

        }).finally(function(){
             $scope.$broadcast('scroll.refreshComplete');
        });

        MainService.getAccountBalance()
            .success(function(res){
                if(res.success && res.attribute){
                    $scope.hotel.money = res.attribute;
                }else{
                    console.log();
                }
            }).error(function(error){

        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    };
    //首页默认值
    $scope.hotel = {
        total: 0,
        isHealthy2: 0,
        isHealthy3: 0,
        isHealthy1: 0,
        money:0
    };

    $timeout(function() {
        ionicMaterialMotion.blinds();
    }, 600);

    $timeout(function() {
        ionicMaterialInk.displayEffect();
    }, 1000);
    //监听用户登录成功状态
    $scope.$on('LOGIN',function(event,data){
        loadData();
    });
    //监听用户登出状态
    $scope.$on('LOGIN_OUT',function(event,data){
        //Push Serv Stop Work
        if (window.cordova) {
            try {
                pushPlugin.stopWork(
                    function (res) {
                        console.log(res);
                    },
                    function (res) {
                        console.error("Push Serv Stop Work failed");
                    }
                );
            } catch(e) {
                //
            }
        }
    });
    $scope.main = {
        refresh : function(){
            init();
        }
    }
    init();
}])
.service('MainService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
    var url = '/crm/crmHotel/';
    return {
        /**
         * 首页数据
         * @param params
         * @returns {*}
         */
        getHotelCount: function () {
            var act = 'getHotelCount',
                params = {};
            params['userid'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 首页余额数据
         * @param params
         * @returns {*}
         */
        getAccountBalance: function () {
            var act = 'getAccountBalance',
                params = {};
            params['userid'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        }
    };
}]);